package com.sasu.university_data.service.impl;


import com.sasu.university_data.dto.Pwd;
import com.sasu.university_data.dto.UpdateUserDto;
import com.sasu.university_data.entity.User;
import com.sasu.university_data.mapper.UserMapper;
import com.sasu.university_data.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service("userService")
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public Integer addUser( User user) {
        //1.用户名不能重复
        String username= user.getUsername();
        User result = userMapper.findByUsername(username);
        if (result!=null){
            //表示用户名已被占用
            return 601;
        }
        //2.密码通过MD5加密
        //获取盐值
        String salt= UUID.randomUUID().toString();
        //后期再加密会用得到，所以先进行存储
        user.setSalt(salt);
        //加密密码
        String md5Password = getMd5Password(salt, user.getPassword());

        user.setPassword(md5Password);

        //3.补全数据
        user.setIsDelete(0);
        user.setCreatedUser(username);
        user.setCreatedTime(new Date());
        user.setModifiedUser(username);
        user.setModifiedTime(new Date());
        //4.插入数据库
        Integer uid = userMapper.insertUser(user);
        if (uid==0){
            //表示数据库出问题了
            return 602;
        }
        //表示注册成功
        return 200;
    }

    /**
     * 加密方法
     *
     * @return {@link String }
     * @author qionglan
     * @date 2024/06/13 10:41:13
     **/

    public String getMd5Password(String salt,String password){
        System.err.println("加密前："+password);
        for (int i=0;i<3;i++){
            password = DigestUtils.md5DigestAsHex((salt + password + salt).getBytes());

        }
        System.err.println("加密后的密码："+password);
        return password;
    }

    @Override
    public Integer login(User user) {

        //1.检查用户名
        String username= user.getUsername();
        User result = userMapper.findByUsername(username);
        if (result==null){
            //用户名不存在
            return 601;
        }

        //2.检查密码
        String sqlPassword= result.getPassword();
        String password = user.getPassword();
        //进行加密
        String md5Password = getMd5Password(result.getSalt(), password);
        //判断两个密码是否一致
        if (!md5Password.equals(sqlPassword)){
            //密码错误
            return 602;
        }

        //3.检查当前账户状态
        Integer state = result.getIsDelete();
        if (state==1){
            //已被封禁
            return 603;
        }
        //允许登录
        return 200;
    }

    @Override
    public Integer changePwd( String username, Pwd pwd) {
        //1.验证原密码
        User result = userMapper.findByUsername(username);
        String sqlPassword= result.getPassword();
        String oldPassword=pwd.getOldPassword();
        String salt = result.getSalt();
        String md5Password = getMd5Password(salt, oldPassword);
        //将新加密的原密码和数据库中存储的密码比对
        if (!md5Password.equals(sqlPassword)){
            //原密码输入错误
            return 601;
        }
        String newPassword = pwd.getNewPassword();
        String newMd5Password = getMd5Password(salt, newPassword);

        //2.替换原密码
        Integer integer = userMapper.updatePassword(username, newMd5Password);
        if (integer==0){
            //修改失败
            return 602;
        }
        //修改成功
        return 603;
    }


    @Override
    public Integer changeAvatar(String username, String avatar) {
        //查询当前用户是否被封禁
        User result = userMapper.findByUsername(username);
        if (result.getIsDelete()==1){
            //用户被封禁
            return 701;
        }
        //补全数据
        Date now=new Date();
        User user=new User();
        user.setUsername(username);
        user.setAvatar(avatar);
        user.setModifiedTime(now);
        user.setModifiedUser(username);
        Integer rows = userMapper.uploadAvatar(user);
        if (rows!=1){
            //数据库出问题了
            return 702;
        }
        //一切正常
        return 703;
    }

    @Override
    public String findAvatarByUsername(String username) {
        User result = userMapper.findByUsername(username);
        return result.getAvatar();
    }

    @Override
    public boolean updateUserByUsername ( UpdateUserDto userDto ) {
        return userMapper.updateUserByUsername(userDto)>0;
    }

    @Override
    public User selectByUsername ( String username ) {
        return userMapper.selectByUsername(username);
    }

    @Override
    public long count ( String username ) {
        return userMapper.count(username);
    }

    @Override
    public List<User> selectByPage ( String username, Integer page, Integer pageSize ) {
        if(page<=1) {
            page = 1;
        }
        if(pageSize<=0) {
            pageSize = 10;
        }
        return userMapper.selectByPage(username,(page - 1) * pageSize, pageSize);
    }

    @Override
    public boolean updateUser ( User user ) {
        return userMapper.updateUser(user)>0;
    }

    @Override
    public boolean insertUser ( User user ) {
        return userMapper.insertUser(user)>0;
    }

    @Override
    public boolean deleteById ( Integer uid ) {
        return userMapper.deleteById(uid)>0;
    }
}
